#include "simple_rest_api.h"
#include "api_decorator.h"
#include <iostream>

// 定义User DTO
struct User {
    int id;
    std::string name;
    std::string email;

    // 将User转换为JSON
    nlohmann::json toJson() const {
        return {
            {"id", id},
            {"name", name},
            {"email", email}
        };
    }

    // 从JSON转换为User
    static User fromJson(const nlohmann::json& j) {
        User user;
        user.id = j["id"].get<int>();
        user.name = j["name"].get<std::string>();
        user.email = j["email"].get<std::string>();
        return user;
    }
};

int main() {
    SimpleRest::RestApiHandler api;
    SimpleRest::ApiDecorator api_decorator(api);
    const std::string data_file = "data.json";

    // 初始化数据文件
    nlohmann::json initial_data = {{"users", nlohmann::json::array()}};

    api_decorator.saveDataToFile(data_file, initial_data)
        .GET("/users").handle([&api, data_file](const SimpleRest::HttpRequest& request) {
            nlohmann::json data = api.loadDataFromFile(data_file);
            return SimpleRest::HttpResponse{200, data["users"]};
        });
        // .POST("/users").handle([&api, data_file](const SimpleRest::HttpRequest& request) {
        //     nlohmann::json data = api.loadDataFromFile(data_file);
        //     User new_user = User::fromJson(request.body);
        //     data["users"].push_back(new_user.toJson());
        //     api.saveDataToFile(data_file, data);
        //     return SimpleRest::HttpResponse{201, new_user.toJson()};
        // });
        // .GET("/users/<id>").handle([&api, data_file](const SimpleRest::HttpRequest& request) {
        //     nlohmann::json data = api.loadDataFromFile(data_file);
        //     auto it = request.params.find("id");
        //     if (it != request.params.end()) {
        //         int user_id = std::stoi(it->second);
        //         for (const auto& user : data["users"]) {
        //             if (user["id"].get<int>() == user_id) {
        //                 return SimpleRest::HttpResponse{200, user};
        //             }
        //         }
        //     }
        //     return SimpleRest::HttpResponse{404, "User not found"};
        // })
        // .PUT("/users/<id>").handle([&api, data_file](const SimpleRest::HttpRequest& request) {
        //     nlohmann::json data = api.loadDataFromFile(data_file);
        //     auto it = request.params.find("id");
        //     if (it != request.params.end()) {
        //         int user_id = std::stoi(it->second);
        //         User updated_user = User::fromJson(request.body);
        //         for (auto& user : data["users"]) {
        //             if (user["id"].get<int>() == user_id) {
        //                 user = updated_user.toJson();
        //                 api.saveDataToFile(data_file, data);
        //                 return SimpleRest::HttpResponse{200, user};
        //             }
        //         }
        //     }
        //     return SimpleRest::HttpResponse{404, "User not found"};
        // })
        // .DELETE("/users/<id>").handle([&api, data_file](const SimpleRest::HttpRequest& request) {
        //     nlohmann::json data = api.loadDataFromFile(data_file);
        //     auto it = request.params.find("id");
        //     if (it != request.params.end()) {
        //         int user_id = std::stoi(it->second);
        //         auto user_it = data["users"].begin();
        //         for (; user_it != data["users"].end(); ++user_it) {
        //             if (user_it->at("id").get<int>() == user_id) {
        //                 break;
        //             }
        //         }
        //         if (user_it != data["users"].end()) {
        //             data["users"].erase(user_it);
        //             api.saveDataToFile(data_file, data);
        //             return SimpleRest::HttpResponse{200, "User deleted"};
        //         }
        //     }
        //     return SimpleRest::HttpResponse{404, "User not found"};
        // });

    // 模拟请求
    api_decorator
        .testRoute(SimpleRest::HttpMethod::GET, "/users")
        .testRoute(SimpleRest::HttpMethod::POST, "/users", User{1, "John Doe", "john@example.com"}.toJson())
        .testRoute(SimpleRest::HttpMethod::GET, "/users/1")
        .testRoute(SimpleRest::HttpMethod::PUT, "/users/1", User{1, "John Smith", "john.smith@example.com"}.toJson())
        .testRoute(SimpleRest::HttpMethod::GET, "/users/1")
        .testRoute(SimpleRest::HttpMethod::DELETE, "/users/1")
        .testRoute(SimpleRest::HttpMethod::GET, "/users");

    return 0;
}